/**
 * 
 */
package com.bolbachchan.blog.dao.common;

import java.sql.Timestamp;
import java.util.Date;
import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bolbachchan.blog.hibernate.domain.UserDetails;

/**
 * @author Chaitanya
 * 
 */
public class UserDetailsDAOImpl extends BaseDAO implements UserDetailsDAO {

    private final static Logger LOG = LoggerFactory.getLogger(UserDetailsDAOImpl.class);

    /*
     * (non-Javadoc)
     * @see
     * com.bolbachchan.blog.dao.common.UserDetailsDAO#getUserDetailsByUsername
     * (String username)
     */
    @Override
    public UserDetails getUserDetailsByUsername(String username) {
	LOG.debug("Entered getUserDetailsByUsername >> UserDetailsDAOImpl");
	UserDetails userDetails = null;
	Session session = null;
	Query query = null;
	try {
	    session = getSessionFactory().getCurrentSession();
	    query = session.getNamedQuery(GET_USER_DETAILS_BY_USERNAME);
	    query.setParameter(USERNAME, username);

	    /*List<UserDetails> list = (List<UserDetails>) query.list();
	    for (UserDetails user : list) {
		if (user.getPosts() != null) {
		    for (Posts post : user.getPosts()) {
			LOG.info(post.toString());
		    }
		}
	    }*/

	    userDetails = (UserDetails) query.uniqueResult();
	}
	catch (Exception e) {
	    LOG.error("Excpetion occurred : " + e);
	}
	finally {
	    if (query != null) {
		query = null;
	    }
	    LOG.debug("Exiting getUserDetailsByUsername >> UserDetailsDAOImpl");
	}
	return userDetails;
    }

    /*
     * (non-Javadoc)
     * @see
     * com.bolbachchan.blog.dao.common.UserDetailsDAO#getUserDetailsById(java
     * .lang.Integer)
     */
    @Override
    public UserDetails getUserDetailsById(Integer objId) {
	LOG.debug("Entered getUserDetailsById >> UserDetailsDAOImpl");

	UserDetails userDetails = null;
	Session session = null;
	Query query = null;
	try {
	    session = getSessionFactory().getCurrentSession();
	    query = session.getNamedQuery(GET_USER_DETAILS_BY_ID);
	    query.setParameter(OBJ_ID, objId);

	    userDetails = (UserDetails) query.uniqueResult();
	}
	catch (Exception e) {
	    LOG.debug("Exception occurred : " + e);
	}
	finally {
	    if (query != null) {
		query = null;
	    }
	    LOG.debug("Exiting getUserDetailsById >> UserDetailsDAOImpl");
	}
	return userDetails;
    }

    /*
     * (non-Javadoc)
     * @see com.bolbachchan.blog.dao.common.UserDetailsDAO#updateLastLogin()
     */
    @Override
    public void updateLastLogin(String username) {
	LOG.debug("Entered updateLastLogin >> UserDetailsDAOImpl");
	Session session = null;
	Query query = null;
	try {
	    session = getSessionFactory().getCurrentSession();
	    query = session.getNamedQuery(UPDATE_LAST_LOGIN);
	    query.setParameter(USERNAME, username).setParameter(LAST_LOGIN, new Timestamp(new Date().getTime()));
	    query.executeUpdate();
	    LOG.debug("Last Login update successful >> updateLastLogin >> UserDetailsDAOImpl");
	}
	catch (Exception e) {
	    LOG.error("Last Login update unsuccessful >> updateLastLogin >> UserDetailsDAOImpl");
	    LOG.error("Exception occurred : " + e);
	}
	finally {
	    if (query != null) {
		query = null;
	    }
	    LOG.debug("Exiting updateLastLogin >> UserDetailsDAOImpl");
	}
    }

    /*
     * (non-Javadoc)
     * @see com.bolbachchan.blog.dao.common.UserDetailsDAO#updateLastLogin()
     */
    @Override
    public void updateLastLogin(UserDetails user) {
	LOG.debug("Entered updateLastLogin >> UserDetailsDAOImpl");
	Session session = null;
	try {
	    session = getSessionFactory().getCurrentSession();
	    user.setLastLogin(new Timestamp(new Date().getTime()));
	    session.update(user);

	    LOG.debug("Last Login update successful >> updateLastLogin >> UserDetailsDAOImpl");
	}
	catch (Exception e) {
	    LOG.error("Last Login update unsuccessful >> updateLastLogin >> UserDetailsDAOImpl");
	    LOG.error("Exception occurred : " + e);
	}
	finally {
	    LOG.debug("Exiting updateLastLogin >> UserDetailsDAOImpl");
	}
    }

}
